package com.wave.system.thread;

import com.wave.common.utils.redis.RedisUtils;
import com.wave.common.utils.spring.SpringUtils;
import com.wave.system.domain.SysAlarmRecord;
import com.wave.system.mapper.SysAlarmRecordMapper;
import com.wave.system.service.ISysProduceRecordService;
import com.wave.system.util.Modbus4jUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class ShipmentFinishListenerThread implements Runnable {


    private SysAlarmRecordMapper sysAlarmRecordMapper = SpringUtils.getBean(SysAlarmRecordMapper.class);

    private ISysProduceRecordService produceRecordService = SpringUtils.getBean(ISysProduceRecordService.class);

    private static final Map<Integer, Integer> map = new HashMap<>();
    static {

        //六个工装架运走的本地逻辑处理完成后，回复plc处理完成
        map.put(0, 4080);
        map.put(1, 4081);
        map.put(2, 4082);
        map.put(3, 4083);
    }

    /**
     * A1~A4 工装架出货完成监听线程
     */
    @Override
    public void run() {
        while (true) {
            try {
                boolean[] booleans = Modbus4jUtils.batchReadCoilStatus(1, 4888, 4);
                for (int i = 0; i < booleans.length; i++) {
                    boolean completeFlag = booleans[i];
                    int lineNumber = i + 1;
                    if (completeFlag) {
                        log.info("plc上报{}号线工装架出货完成", lineNumber);
                        // 拼接厂线的redisKey，用于获取厂线目前待出货的所有物料编码集合
                        String redisKey = "A" + lineNumber + "_SHELF_TOTAL";
                        // 获取厂线工装架放置的产品编号，保存生产记录
                        List<String> snCodes = RedisUtils.getCacheObject(redisKey);
                        if (CollectionUtils.isEmpty(snCodes)) {
                            log.info("A{}线工装架出货,无法获取叠盘机目前的物料sn码集合,直接跳过", lineNumber);
                            Modbus4jUtils.writeCoil(1, map.get(i), true);
                            continue;
                        }
                        Boolean isSuccess = produceRecordService.batchSaveBySnCodes(snCodes, "shelfNumber");
                        if (!isSuccess) {
                            SysAlarmRecord alarmRecord = new SysAlarmRecord();
                            alarmRecord.setAlarmTime(new Date());
                            alarmRecord.setAlarmType(1);
                            alarmRecord.setAlarmContent("A" + lineNumber + "线出货完成插入生产记录失败");
                            sysAlarmRecordMapper.insert(alarmRecord);
                        } else {
                            log.info("A{}线出架完成,删除redis中该厂线的物料码总集合", lineNumber);
                            RedisUtils.deleteObject(redisKey);
                            //处理完逻辑回复plc处理完成
                            Modbus4jUtils.writeCoil(1, map.get(i), true);
                        }
                    }
                }
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (Exception e) {
                log.error("A1~A4 工装架出货完成监听异常", e);
            }
        }
    }
}
